package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
 * @Author:大力王
 * @Date:2024/3/10-10-8:02
 * 开始了
 */
@RestController
public class EmailVerificationController {
    @Autowired
    JavaMailSender emailSender;

    @PostMapping("/sendEmail")
    public String sendEmail(@RequestParam String email) {
        // 生成随机验证码
        String verificationCode = "123456"; // 替换为实际的代码生成逻辑

        // 发送带有验证码的电子邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3032306978@qq.com");
        message.setTo(email);
        message.setSubject("Verification Code");
        message.setText("Your verification code is: " + verificationCode);
        emailSender.send(message);

        return "Verification code sent to " + email;
    }

    @PostMapping("/verifyCode")
    public String verifyCode(@RequestParam String email, @RequestParam String code) {
        // 在此处添加代码验证逻辑
        if ("123456".equals(code)) { // 替换为实际验证逻辑
            return "Verification successful for " + email;
        } else {
            return "Invalid verification code";
        }
    }
}
